﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WITM.DAL;

namespace WITM.BLL
{
    public class SpendingCategoryBL
    {
        private IUnitOfWork unitOfWork;

        public SpendingCategoryBL(IUnitOfWork unitOfWork)
        {
            this.unitOfWork = unitOfWork;
        }

        public IEnumerable<SpendingCategories> GetAll()
        {
            return unitOfWork.SpendingCategoryRepository.GetAll();
        }

        public SpendingCategories GetCategoryByID(long categoryID)
        {
            return unitOfWork.SpendingCategoryRepository.First(t => t.CategoryID == categoryID);
        }

        public IEnumerable<SpendingCategories> GetAllPerUser(int userID)
        {
            return unitOfWork.SpendingCategoryRepository.Find(t => t.Users.UserID == userID);
        }

        public void Add(SpendingCategories category)
        {
            SpendingCategories checkCategory = unitOfWork.SpendingCategoryRepository.First(
                    c => c.Users.UserID == category.Users.UserID
                        && c.Description == category.Description);
            if (checkCategory == null)
            {
                unitOfWork.SpendingCategoryRepository.Add(category);
                unitOfWork.Commit();
            }
            else
            {
                throw new Exception("Category with specific name already exists for that user");
            }
        }
    }
}
